package com.android.equipment.adapter

import android.graphics.Color
import android.view.View
import android.widget.LinearLayout
import android.widget.RadioGroup
import androidx.recyclerview.widget.RecyclerView
import com.android.equipment.R
import com.android.equipment.bean.RealDataBean
import com.android.equipment.dao.WorkDetail
import com.android.equipment.dao.operate.DaoRealTimeAlam
import com.android.equipment.dao.operate.DaoWorkDetail
import com.blankj.utilcode.util.GsonUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent

class WorkContactAdapter(layoutRes: Int, val workId: Long, var workStatus: Int) :
    BaseQuickAdapter<WorkDetail, BaseViewHolder>(layoutRes) {
    private var isEdit = true

    //private var deviceNodeAdapter: DeviceNodeAdapter? = null
    fun setEditState(isEdit: Boolean) {
        this.isEdit = isEdit
        notifyDataSetChanged()
    }

    override fun convert(holder: BaseViewHolder, item: WorkDetail) {
        holder.setText(R.id.tv_device_code, item.deviceCode)
        holder.setText(R.id.tv_device_change_time, item.deviceDataChangeTime)
        //setDeviceNodeData(holder, item)
        holder.setText(R.id.tv_people_name, item.peopleName)
        holder.setText(R.id.tv_people_phone, item.peoplePhone)
        if (!isEdit) {
            holder.getView<LinearLayout>(R.id.rg_state).visibility = View.GONE
        } else {
            holder.getView<LinearLayout>(R.id.rg_state).visibility = View.VISIBLE
        }

        holder.setVisible(R.id.iv_alam,item.isErrorAlam)

        when (item.peopleCondition) {
            1 -> {
                holder.getView<RadioGroup>(R.id.rg_state).check(R.id.rb_normal)
            }

            2 -> {
                holder.getView<RadioGroup>(R.id.rg_state).check(R.id.rb_unusual)
            }

            else -> {
                holder.getView<RadioGroup>(R.id.rg_state).clearCheck()
            }
        }
        holder.getView<RadioGroup>(R.id.rg_state).setOnCheckedChangeListener { _, checkedId ->
            if (checkedId == R.id.rb_normal) {
                item.peopleCondition = 1
            } else {
                item.peopleCondition = 2
            }
            if (workStatus == 1) {
                DaoWorkDetail.getInstance().updateWorkDetail(item)
            }
        }
        //if (workStatus == 1){
        val realTimeAlamList = DaoRealTimeAlam.getInstance()
            .getRealTimeAlamByWorkIdAndDeviceCode(workId, item.deviceCode)

        val type = GsonUtils.getListType(RealDataBean::class.java)
        val realDataList: MutableList<RealDataBean> = GsonUtils.fromJson(item.deviceNodeJson, type)
        val map = realDataList.map { it.node }
        val nodeList: MutableList<String> = mutableListOf()
        realTimeAlamList.forEachIndexed { index, realTimeAlam ->
            if (map.contains(realTimeAlam.node)){
                nodeList.add(realTimeAlam.node_des)
            }
        }

       // holder.getView<LinearLayout>(R.id.ll_alam_info).visibility = if (nodeList.size > 0) {View.VISIBLE} else {View.GONE}

        if (nodeList.size > 0) {
            item.isHaveAlam = true
            holder.itemView.setBackgroundResource(R.drawable.shape_radius_8_solid_ffeae8)
            val sb: StringBuilder = StringBuilder()
            nodeList.forEach { node ->
                if (nodeList.size > 1){
                    sb.append("${node}丶")
                }else {
                    sb.append(node)
                }
            }

            holder.setTextColor(R.id.tv_alam_node, Color.parseColor("#ED3241"))
            holder.setImageResource(R.id.iv_look, R.mipmap.icon_red_go)
            holder.setText(R.id.tv_alam_node, sb.toString())
//            holder.getView<RecyclerView>(R.id.rv_alam)
//                .setBackgroundResource(R.drawable.shape_radius_8_solid_white)

        } else {
            item.isHaveAlam = false
            holder.itemView.setBackgroundResource(R.drawable.shape_radius_8_solid_white)

            holder.setTextColor(R.id.tv_alam_node, Color.parseColor("#4E5969"))
            holder.setImageResource(R.id.iv_look, R.mipmap.icon_black_go)
            holder.setText(R.id.tv_alam_node, "查看设备节点信息")
/*            holder.getView<RecyclerView>(R.id.rv_alam)
                .setBackgroundResource(R.drawable.shape_radius_8_solid_f8f9fa)*/
        }


//        }else {
//            holder.itemView.setBackgroundResource(R.drawable.shape_radius_8_solid_white)
//            holder.getView<RecyclerView>(R.id.rv_alam).setBackgroundResource(R.drawable.shape_radius_8_solid_f8f9fa)
//        }

    }


//    private fun setDeviceNodeData(holder: BaseViewHolder, item: WorkDetail) {
//        val rvAlam = holder.getView<RecyclerView>(R.id.rv_alam)
//        val flex = FlexboxLayoutManager(context).apply {
//            flexDirection = FlexDirection.ROW
//            flexWrap = FlexWrap.WRAP
//            justifyContent = JustifyContent.FLEX_START
//        }
//        rvAlam.layoutManager = flex
//        deviceNodeAdapter = DeviceNodeAdapter(workStatus, workId, item.deviceCode)
//        rvAlam.adapter = deviceNodeAdapter
//        val type = GsonUtils.getListType(RealDataBean::class.java)
//        val realDataList: MutableList<RealDataBean> = GsonUtils.fromJson(item.deviceNodeJson, type)
//        deviceNodeAdapter!!.setList(realDataList)
//
//    }

    fun updateAdapterWorkState(workStatus: Int) {
        this.workStatus = workStatus
    }

//    private fun <T> List<T>.findIndexed(predicate: (T) -> Boolean): Pair<T?, Int> {
//        for ((index, element) in this.withIndex()) {
//            if (predicate(element)) {
//                return Pair(element, index)
//            }
//        }
//        return Pair(null, -1)  // 如果没有找到，返回 null 和 -1
//    }
}